Imports System.Configuration
Imports System.Web.Configuration

Public Class LookupValueService
    Private Shared _provider As LookupValueProvider = Nothing
    Private Shared _providers As LookupValueCollection = Nothing
    Private Shared _lock As Object = New Object
    Private Shared isInitialized As Boolean = False
    Private Shared initializationException As Exception

    Public ReadOnly Property Provider() As LookupValueProvider
        Get
            Return _provider
        End Get
    End Property

    Public ReadOnly Property Providers() As LookupValueCollection
        Get
            Return _providers
        End Get
    End Property

    Private Shared Sub LoadProviders()
        Try
            'Get the feature's configuration info
            Dim qc As LookupValueConfiguration = CType(ConfigurationManager.GetSection("LookupValueProvider"), LookupValueConfiguration)
            If ((qc.DefaultProvider Is Nothing) _
                        OrElse ((qc.Providers Is Nothing) _
                        OrElse (qc.Providers.Count < 1))) Then
                Throw New System.Configuration.Provider.ProviderException(("You must specify a valid default provider."))
            End If
            _providers = New LookupValueCollection
            ProvidersHelper.InstantiateProviders(qc.Providers, _providers, GetType(LookupValueProvider))
            _providers.SetReadOnly()
            _provider = _providers(qc.DefaultProvider)
            If (_provider Is Nothing) Then
                Throw New ConfigurationErrorsException("You must specify a default provider for the feature.", qc.ElementInformation.Properties("defaultProvider").Source, qc.ElementInformation.Properties("defaultProvider").LineNumber)
            End If
        Catch ex As Exception
            initializationException = ex
            isInitialized = True
            Throw ex
        End Try
        isInitialized = True
        'error-free initialization
    End Sub
    'Lookup Category Methods
    Public Shared Function createLookupCategory(ByVal ShortDesc As String, ByVal LongDesc As String, ByVal CurrentUser As String) As String
        LoadProviders()
        Return _provider.createLookupCategory(ShortDesc, LongDesc, CurrentUser)
    End Function
    Public Shared Function getLookupCategories() As Generic.List(Of LookupCategoryDetails)
        LoadProviders()
        Return _provider.getLookupCategories()
    End Function
    Public Shared Function getLookupCategories(ByVal SortExpression As String, ByVal SortOrder As KDESolutions.Providers.DataProvider.SortOrder) As Generic.List(Of LookupCategoryDetails)
        LoadProviders()
        Return _provider.getLookupCategories(SortExpression, SortOrder)
    End Function
    Public Shared Function searchLookupCategories(ByVal SearchCollection As Generic.List(Of KDESolutions.Providers.DataProvider.SearchCriteria)) As Generic.List(Of LookupCategoryDetails)
        LoadProviders()
        Return _provider.searchLookupCategories(SearchCollection)
    End Function
    Public Shared Sub updateLookupCategory(ByVal CategoryId As String, ByVal ShortDesc As String, ByVal LongDesc As String, ByVal SortOrder As Integer, ByVal CurrentUser As String)
        LoadProviders()
        _provider.updateLookupCategory(CategoryId, ShortDesc, LongDesc, SortOrder, CurrentUser)
    End Sub
    Public Shared Sub deleteLookupCategory(ByVal CategoryId As String, ByVal CurrentUser As String)
        LoadProviders()
        _provider.deleteLookupCategory(CategoryId, CurrentUser)
    End Sub

    Public Shared Sub ReOrderLookupCategory(ByVal CategoryId As String, ByVal SortOrder As Integer, ByVal ShortDesc As String)
        LoadProviders()
        _provider.ReOrderLookupCategory(CategoryId, SortOrder, ShortDesc)
    End Sub

    Public Shared Function GetLookupCategoriesForReOrder() As Generic.List(Of KDESolutions.Providers.LookupValues.LookupBindingCategories)
        LoadProviders()
        Return _provider.GetLookupCategoriesForReOrder()
    End Function


    'Lookup Value methods
    Public Shared Function createLookupValue(ByVal CategoryId As String, ByVal ValueCode As String, ByVal ShortDesc As String, ByVal LongDesc As String, ByVal CurrentUser As String, Optional ByVal SortOrder As Integer = 0, Optional ByVal ParentId As String = "") As String
        LoadProviders()
        Return _provider.createLookupValue(CategoryId, ValueCode, ShortDesc, LongDesc, CurrentUser, SortOrder, ParentId)
    End Function
    Public Shared Function getLookupValues(ByVal CategoryId As String) As Generic.List(Of LookupValueDetails)
        LoadProviders()
        Return _provider.getLookupValues(CategoryId)
    End Function
    Public Shared Function getLookupValues(ByVal CategoryId As String, ByVal SortExpression As String, ByVal SortOrder As KDESolutions.Providers.DataProvider.SortOrder) As Generic.List(Of LookupValueDetails)
        LoadProviders()
        Return _provider.getLookupValues(CategoryId, SortExpression, SortOrder)
    End Function
    Public Shared Function getLookupSubValues(ByVal ValueParentId As String) As Generic.List(Of LookupValueDetails)
        LoadProviders()
        Return _provider.getLookupSubValues(ValueParentId)
    End Function
    Public Shared Function searchLookupValues(ByVal SearchCollection As Generic.List(Of KDESolutions.Providers.DataProvider.SearchCriteria)) As Generic.List(Of LookupValueDetails)
        LoadProviders()
        Return _provider.searchLookupValues(SearchCollection)
    End Function
    Public Shared Sub updateLookupValue(ByVal ValueId As String, ByVal CategoryId As String, ByVal ValueCode As String, ByVal ShortDesc As String, ByVal LongDesc As String, ByVal CurrentUser As String, Optional ByVal ParentId As String = "", Optional ByVal SortOrder As Integer = 0)
        LoadProviders()
        _provider.updateLookupValue(ValueId, CategoryId, ValueCode, ShortDesc, LongDesc, CurrentUser, ParentId, SortOrder)
    End Sub
    Public Shared Sub deleteLookupValue(ByVal ValueId As String, ByVal CurrentUser As String)
        LoadProviders()
        _provider.deleteLookupValue(ValueId, CurrentUser)
    End Sub

    Public Shared Sub ReOrderLookupValue(ByVal ValueId As String, ByVal SortOrder As Integer, ByVal ShortDesc As String)
        LoadProviders()
        _provider.ReOrderLookupValue(ValueId, SortOrder, ShortDesc)
    End Sub

    Public Shared Function GetLookupValuesForReOrder() As Generic.List(Of KDESolutions.Providers.LookupValues.LookupBindingValues)
        LoadProviders()
        Return _provider.GetLookupValuesForReOrder
    End Function
End Class